/*
 * @Author: huang liang miao 1127255342@qq.com
 * @Date: 2024-11-29 09:46:24
 * @LastEditors: huang liang miao 1127255342@qq.com
 * @LastEditTime: 2025-01-08 17:00:11
 * @FilePath: \dcy-webd:\webCode\dcyCode\scientific_research_manager_web\src\utils\excelResult.ts
 * @Description: 导入结果数据处理
 */

import { DictionaryCreateReqDTO } from '@/api/system/dict/type'
import formateDate from '@/utils/formateDate'
import { AccountInfoDTO } from '@/api/system/user/type'

// 字典导入
export function dictResult(res: any) {
  const batchRows = <DictionaryCreateReqDTO[]>([])
  res[0].data?.forEach((item: any) => {
    batchRows.push({
      name: item[0],
      dataName: item[1],
      dataRemark: item[2],
    })
  })

  return {
    batchRows,
  }
}

// 用户导入
export function userResult(res: any) {
  const accountInfos = <AccountInfoDTO[]>([])
  res[0].data?.forEach((item: any) => {
    accountInfos.push({
      code: item[0],
      name: item[1],
      phone: item[2],
      email: item[3],
      roleName: item[4]?.split('，'),
      identityCode: item[5]?.split('，'),
      inside: item[6] === '是' ? true : false,
      departmentName: item[7],
      sex: item[8] === '男' ? 1 : item[8] === '女' ? 2 : '',
      birthday: (item[9] && isParseableDate(formateDate(new Date(item[9])))) ? formateDate(new Date(item[9])).format('YYYY-MM-DD') : '',
      educationName: item[10],
      academicName: item[11],
      title: item[12],
      positionType: item[13],
      positionLevel: item[14],
      subject: item[15],
      organizationName: item[6] === '是' ? '南昌交通学院' : item[16],
      direction: item[17],
      idcard: item[18],
    })
  })

  return {
    accountInfos,
  }
}
// 判断字符串是否能被解析为日期
function isParseableDate(str) {
  const timestamp = Date.parse(str);
  return !isNaN(timestamp);
}

// 返回导入结果
export const importSuccess = ref(false)
export function result(res){
  if (res.data.type === 'application/json') {
    ElMessage({
      message: '导入成功',
      type: 'success',
    })
    importSuccess.value = true
  }
  else {
    ElMessage({
      message: '导入失败，请根据返回的失败报错文件修改后再上传',
      type: 'error',
    })
    importSuccess.value = false
    downloadExecl(res as Blob)
  }
}

const mimeTypes = {
  docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  doc: 'application/msword',
  csv: 'text/csv',
  xls: 'application/vnd.ms-excel',
  xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  zip: 'application/zip',
  txt: 'text/plain',
}
export function downloadExecl(res: any = {}) {
  const defaultType = mimeTypes.docx
  const contentDisposition = res.headers['content-disposition']
  //  let type = res.headers["content-type"];
  const fileName = contentDisposition.split(';')[1].split('=')[1]
  // fileName = decodeURI(fileName)
  // const type = fileName.split('.').pop() || defaultType
  const type = res.data.type || defaultType
  const blob = new Blob([res.data], { type: `${type};charset=UTF-8`, contentDisposition })
  const downloadElement = document.createElement('a')
  const href = window.URL.createObjectURL(blob) // 创建导出链接
  downloadElement.href = href
  downloadElement.download = decodeURIComponent(fileName) // 导出文件名
  document.body.appendChild(downloadElement)
  downloadElement.click() // 点击导出
  document.body.removeChild(downloadElement) // 导出完成移除元素
  window.URL.revokeObjectURL(href) // 释放blob对象
}
